/**
 * cookie
 * @author: Calvin
 * @create: 2017-10-26 09:34
 */

export default {
    /**
     * 设置 cookie
     * @param name cookie 名称
     * @param value cookie 值
     * @param expiresDay 失效时间 单位小时
     */
    setCookie (name, value, expiresDay){
        let str = name + "=" + escape(value);
        if (expiresDay > 0) {
            let date = new Date();
            let ms = expiresDay * 3600 * 1000;
            date.setTime(date.getTime() + ms);
            str += "; expires=" + date.toGMTString();
        }
        document.cookie = str;
    } ,

    /**
     * 获取 cookie
     * @param name cookie 名称
     * @returns {*}
     */
    getCookie (name) {
        let arrStr = document.cookie.split("; ");
        for (let i = 0; i < arrStr.length; i++) {
            let temp = arrStr[i].split("=");
            if (temp[0] == name)
                return unescape(temp[1]);
        }
    },

    /**
     * 删除 cookie
     * @param name cookie 名称
     */
    delCookie (name) {
        var date = new Date();
        date.setTime(date.getTime() - 10000);
        document.cookie = name + "=a; expires=" + date.toGMTString();
    }
}
